import 'package:flutter/material.dart';
import '../animation/textLuminescence.dart';
import '../animation/xSpring.dart';
import '../mixin/buttonSwitch.dart';



import 'package:flutter/services.dart';
//MethodChannel
const methodChannel = const MethodChannel('samples.flutter.dev/test');



// 代码块 statelessW
class Home extends StatelessWidget  {
  const Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return orderBody();
  }
}

class orderBody extends StatefulWidget {
  @override
  _DatePickerExampleState createState() => new _DatePickerExampleState();
}

/// RestorationProperty objects can be used because of RestorationMixin.
class _DatePickerExampleState extends State<orderBody> with SingleTickerProviderStateMixin ,WidgetsBindingObserver,buttonSwitch  {
  Map<String,double> popacity = {
    "p1":0,
    "p2":0,
    "p3":0,
    "p4":0,
    "p5":0,
  };

  List<double> pListAnim = [0,0,0,0,0,0,0,0,0,0,0,0];

  List timer = [];


  initAnim(){
    for(var i=0;i<pListAnim.length;i++){
      Future.delayed(Duration(milliseconds: (150*i)), () {
        setState(() {
          pListAnim[i] = 2;
        });
      });
    }
  }

  endAnim(){

    for(var i=0;i<pListAnim.length;i++){
      Future.delayed(Duration(milliseconds: (150*i)), () {
        setState(() {
          pListAnim[i] = 1;
        });
      });
    }

  }


  pushRoute(context,path){
    if(getButtonState( key:'login')){
      setButtonState( key:'login', switchBool:false);
      endAnim();
      Future.delayed(Duration(milliseconds: 500), () {
        setState(() {
          bgSize = 2000;
          bgRadius = 0;
        });
        Future.delayed(Duration(milliseconds:400), () {
          Navigator.pushNamed(context, path).then((v){
            setState(() {
              bgSize = 300;
              bgRadius = 99999;
            });
            initAnim();
            setButtonState( key:'login', switchBool:true);
          });
        });

      });
    }



  }

  token() async{
    final String token =  await methodChannel.invokeMethod('getSharedPreferences','token');
    //await methodChannel.invokeMethod('deleteSharedPreferences','token');



    //methodChannel.invokeMethod('deleteSharedPreferences','token');
    if(token != ''){
      Navigator.of(context).pushNamedAndRemoveUntil( '/app', ModalRoute.withName('/app'));
      // Response response = await webTokenHttp(path:'competition-app/public/api/users/login');
      // if(response.statusCode == 200){
      //   Map<String, dynamic> userMap = jsonDecode(response.body);
      //   if(userMap['code'] == 1){
      //     print(prefs.getString('登陆成功'));
      //   }
      // }
    }
  }


  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    WidgetsBinding.instance.addObserver(this);

    token();

    for(var i=1;i<=4;i++){
      Future.delayed(Duration(milliseconds: 500+(300*i)), () {
        setState(() {
          popacity['p'+(i.toString())] = 1;
        });
      });
    }

  }

  double bgSize = 300;
  double bgRadius = 99999;

  @override
  Widget build(BuildContext context) {


    final rem =  MediaQuery.of(context).size.width/1080;
    return  Scaffold(
      body: Container(
        width: 1080*rem,
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/images/qq9_introduction_poster.jpg'),
            fit: BoxFit.cover,
          ),
        ),
        child:Stack(
          alignment: Alignment.center,
          children: [
            AnimatedOpacity(
              opacity:popacity['p4'] as double,
              duration:  Duration( milliseconds: 1000),
              child:
            Align(
                alignment: Alignment(0,1-(600/1080)),
                child: TextButton.icon(
                icon:  Icon(Icons.account_circle,color: Colors.white,size: 58*rem,),
                label: Text('登陆',style: TextStyle(fontSize: 58*rem,color: Color.fromRGBO(236, 243, 247, 1))),
                onPressed: (){
                  pushRoute(context,'/login');
                },
            ))),



            Align(
                alignment: Alignment(0,0-(600/1080)),
                child:AnimatedContainer(
                  curve: Curves.easeInBack,
                  duration:  Duration(milliseconds: 300),
                  width:bgSize*rem,
                  height:bgSize*rem,
                  decoration: BoxDecoration(
                      image: DecorationImage(
                        image: AssetImage('assets/images/bg2.jpg'),
                        fit: BoxFit.cover,
                      ),
                      borderRadius: BorderRadius.circular(bgRadius)
                  ),
                )
            ),

            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [

                xSpring(
                  state:pListAnim[0] as double,
                  child: AnimatedOpacity(
                    opacity:popacity['p1'] as double,
                    duration: const Duration( milliseconds: 1000),
                    child: Hero(
                      tag:'世界技能大赛',
                      child:Text('世界技能大赛',style: TextStyle(fontWeight: FontWeight.normal,decoration: TextDecoration.none,fontSize: 78*rem,color: Color.fromRGBO(236, 243, 247, 1))),
                    ),
                  ),
                ),


                xSpring(
                  state:pListAnim[1] as double,
                  child:
                AnimatedOpacity(
                  opacity:popacity['p2'] as double,
                  duration: const Duration( milliseconds: 1000),
                  child:
                Hero(
                  tag:'冯俊杰制作',
                  child:Text('冯俊杰制作',style: TextStyle(fontWeight: FontWeight.normal,decoration: TextDecoration.none,fontSize: 38*rem,color: Color.fromRGBO(236, 243, 247, 1))),
                ))),
                xSpring(
                  state:pListAnim[2] as double,
                  child:AnimatedOpacity(
                  opacity:popacity['p3'] as double,
                  duration: const Duration( milliseconds: 1000),
                  child:
                textLuminescence(
                  text:'App Project',
                  fontSize:140.0*rem
                ),)),

                Container(
                  margin: EdgeInsets.fromLTRB(0, 380*rem, 0, 0),

                ),
              ],
            )
          ],
        )
      )
    );
  }
}

